package logger

import "fmt"

// Level defines the priority of a log message.
// When a logger is configured with a level, any log message with a lower
// log level (smaller by integer comparison) will not be output.
type Level int

// The levels of logs.
const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
)

var strs = []string{
	"TRACE",
	"DEBUG",
	" INFO",
	" WARN",
	"ERROR",
}

func (lv Level) toString() string {
	if lv >= LevelTrace && lv <= LevelError {
		return strs[lv]
	}
	return fmt.Sprintf("[?%d] ", lv)
}

func parseLevel(level string) Level {
	switch level {
	case "trace", "TRACE":
		return LevelTrace
	case "debug", "DEBUG":
		return LevelDebug
	case "info", "INFO":
		return LevelInfo
	case "warn", "WARN":
		return LevelWarn
	case "error", "ERROR":
		return LevelError
	default:
		return LevelInfo
	}
}

// Logger is a logger interface that provides logging function with levels.
type Logger interface {
	Name() string

	Level() Level

	SetLevel(lv Level)

	TraceEnabled() bool
	DebugEnabled() bool
	InfoEnabled() bool
	WarnEnabled() bool
	ErrorEnabled() bool

	Trace(v ...interface{})
	Debug(v ...interface{})
	Info(v ...interface{})
	Warn(v ...interface{})
	Error(v ...interface{})

	Tracef(format string, v ...interface{})
	Debugf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Warnf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
}
